import tkinter as tk
from tkinter import messagebox
import random

# 预设的词汇列表
words = [
    "苹果", "香蕉", "汽车", "飞机", "猫咪", "狗狗",
    "书本", "电脑", "雨伞", "足球", "篮球"
]

# 随机选择一个词汇
secret_word = random.choice(words)

# 绘画板类
class DrawingBoard:
    def __init__(self, root):
        self.root = root
        self.canvas = tk.Canvas(root, width=400, height=400, bg="white")
        self.canvas.pack()
        self.prev_x = None
        self.prev_y = None
        self.canvas.bind("<B1-Motion>", self.paint)
        self.canvas.bind("<ButtonRelease-1>", self.reset)

    def paint(self, event):
        x, y = event.x, event.y
        if self.prev_x and self.prev_y:
            self.canvas.create_line((self.prev_x, self.prev_y, x, y), width=2, fill="black")
        self.prev_x = x
        self.prev_y = y

    def reset(self, event):
        self.prev_x = None
        self.prev_y = None

# 猜词区域类
class GuessArea:
    def __init__(self, root):
        self.root = root
        self.label = tk.Label(root, text="请输入你的猜测：")
        self.label.pack()
        self.entry = tk.Entry(root)
        self.entry.pack()
        self.button = tk.Button(root, text="提交猜测", command=self.check_guess)
        self.button.pack()

    def check_guess(self):
        guess = self.entry.get()
        if guess == secret_word:
            messagebox.showinfo("结果", "恭喜你，猜对啦！游戏结束。")
            self.root.destroy()
        else:
            messagebox.showinfo("结果", "猜错啦，再试试吧。")

# 主程序
if __name__ == "__main__":
    root = tk.Tk()
    root.title("你画我猜游戏")

    drawing_board = DrawingBoard(root)
    guess_area = GuessArea(root)

    root.mainloop()